﻿using BL.Models.Account;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace UIServices
{
    public class AccountManager
    {
        private Accounts.ILogin<User> loginService;

        public AccountManager(Accounts.ILogin<BL.Models.Account.User> loginService)
        {
            this.loginService = loginService;
        }

        public enum eCredentialsStatus
        {
            Ok,
            UserIsNotAuntenicated,
            PasswordIsNotOk,
            UserNotFound
        }

        public eCredentialsStatus LogIn(string userName, string password, bool stayConnected = false)
        {
            User user = User.GetByUserName(userName);
            if (user == null)
            {
                return eCredentialsStatus.UserNotFound;
            }

            var credentialsStatus = checkLoginCredentials(user, password);
            if (credentialsStatus == eCredentialsStatus.Ok)
            {
                this.loginService.Login(user, stayConnected);
            }

            return credentialsStatus;
        }

        public eCredentialsStatus checkLoginCredentials(User user, string password)
        {
            if (!isPasswordMatch(user, password)) return eCredentialsStatus.PasswordIsNotOk;
            if (!user.IsAuturize) return eCredentialsStatus.UserIsNotAuntenicated;
            return eCredentialsStatus.Ok;
        }

        private bool isPasswordMatch(User user, string password)
        {
            return encryptPassword(password) == user.EncPassword;
        }

        private string encryptPassword(string pass)
        {
            MD5 md5 = new MD5CryptoServiceProvider();

            //compute hash from the bytes of text
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(pass));

            //get hash result after compute it
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }
            return strBuilder.ToString();
        }

        public bool RegisterUser(User user, string password)
        {
            try
            {
                if (registrationValidation(user, password))
                {
                    user.EncPassword = encryptPassword(password);

                    User.AddNewUser(user);

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        private bool registrationValidation(User user, string pass)
        {
            return User.CheckIfNewUserIsValid(user);
        }

        public User GetCurrentUser()
        {
            return loginService.GetLoggedUser();
        }


        public void LogOut()
        {
            loginService.SignOut();
        }
    }
}
